<?php

namespace app\admin\controller\expert;

use app\common\controller\Backend;
use fast\Tree;
use think\Cache;

/**
 *  每月订单流水分红配置管理
 *
 * @icon   fa fa-list
 * @remark 
 */
class OrderflowMonthConfig extends Backend
{

    protected $searchFields = 'id';

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 列表
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list_info = db("user_orderflow_month_config")
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);
            $list = $list_info->items();
            $total = $list_info->total();
            $result = array("total" => $total, "rows" => $list);
            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 添加
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $this->token();

            $params = $this->request->post("row/a", [], 'strip_tags');
            if ($params) {

                $month = date("Ym", time());
                $user_orderflow_month_config = db("user_orderflow_month_config")->where("month", $month)->find();
                if ($user_orderflow_month_config) {
                    $this->error("已存在相同月份的设置");
                }
                $params["month"] = $month;
                $params["addtime"] = time();

                $result = db("user_orderflow_month_config")->insert($params);
                if ($result === false) {
                    $this->error("添加失败");
                }
                $this->success();
            }
            $this->error();
        }

        return $this->view->fetch();
    }

    /**
     * 编辑
     */
    public function edit($ids = null)
    {
        $row = db("user_orderflow_month_config")->where("id", $ids)->find();
        if (!$row) {
            $this->error(__('No Results were found'));
        }

        if ($this->request->isPost()) {
            $this->token();

            $params = $this->request->post("row/a", [], 'strip_tags');
            if ($params) {
                $result = db("user_orderflow_month_config")->where("id", $ids)->update($params);
                if ($result === false) {
                    $this->error($row->getError());
                }
                $this->success();
            }
            $this->error();
        }


        $this->view->assign("row", $row);

        return $this->view->fetch();
    }

    /**
     * 删除
     */
    public function del($ids = "")
    {
        if (!$this->request->isPost()) {
            $this->error(__("Invalid parameters"));
        }
        $ids = $ids ? $ids : $this->request->post("ids");
        $row = db("user_orderflow_month_config")->where("id", $ids)->find();
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $result = db("user_orderflow_month_config")->where("id", $ids)->delete();
        if ($result === false) {
            $this->error($row->getError());
        }
        $this->success();
    }

    /**
     * 开启
     */
    public function open($ids = "")
    {
        if (!$this->request->isPost()) {
            $this->error(__("Invalid parameters"));
        }
        $ids = $ids ? $ids : $this->request->post("ids");
        $row = db("user_orderflow_month_config")->where("id", $ids)->find();
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $result = db("user_orderflow_month_config")->where("id", $ids)->update(["start_status" => 1]);
        if ($result === false) {
            $this->error($row->getError());
        }
        $this->success();
    }

    /**
     * 关闭
     */
    public function close($ids = "")
    {
        if (!$this->request->isPost()) {
            $this->error(__("Invalid parameters"));
        }
        $ids = $ids ? $ids : $this->request->post("ids");
        $row = db("user_orderflow_month_config")->where("id", $ids)->find();
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $result = db("user_orderflow_month_config")->where("id", $ids)->update(["start_status" => 0]);
        if ($result === false) {
            $this->error($row->getError());
        }
        $this->success();
    }
}
